package com.example.horizontalrefresh;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.horizontalrefresh.adapter.TestDataAdapter;
import com.example.horizontalrefresh.entity.TestData;
import com.example.horizontalrefresh.view.SmartRefreshHorizontal;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private SmartRefreshHorizontal refreshLayout;
    private RecyclerView recycleView;
    private ImageView lastPage, nextPage;
    private TextView textPageNum;
    private int pageIndex = 1;
    private List<TestData> testData;
    private TestDataAdapter testDataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        setListener();
    }

    private void initViews() {
        refreshLayout = findViewById(R.id.refreshLayout);
        recycleView = findViewById(R.id.recycleView);
        ClassicsHeader header = findViewById(R.id.header);
        header.setEnableLastTime(false);

        lastPage = findViewById(R.id.last_page);
        nextPage = findViewById(R.id.next_page);
        textPageNum = findViewById(R.id.text_page_num);
        textPageNum.setText(String.valueOf(pageIndex));
        testData = new ArrayList<>();

    }

    private void setListener() {
        lastPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pageIndex > 1) {
                    setData(pageIndex - 1, true);
                } else {
                    refreshLayout.finishRefresh(true);
                }
            }
        });
        nextPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setData(pageIndex + 1, false);
            }
        });

        testDataAdapter = new TestDataAdapter(MainActivity.this, testData);
        recycleView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recycleView.setAdapter(testDataAdapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (pageIndex > 1) {
                    setData(pageIndex - 1, true);
                } else {
                    refreshLayout.finishRefresh(true);
                }
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                setData(pageIndex + 1, false);
            }
        });
        setData(pageIndex, true);
    }

    private void setData(int page, boolean isfresh) {
        Log.i("ZHY", "setData: "+page);
        testData.clear();
        int data1=page*10;
        int pageData = data1 + 30;
        for (int m =data1; m < pageData; m++) {
            TestData td = new TestData("ZHY", "zhy" + m);
            testData.add(td);
        }
        Log.i("ZHY", "setData: "+testData.size());
        testDataAdapter.notifyDataSetChanged();
        if (isfresh) {
            refreshLayout.finishRefresh(true);
        } else {
            refreshLayout.finishLoadMore(true);
        }
        pageIndex=page;
        textPageNum.setText(String.valueOf(pageIndex));
    }
}
